//http://www.developer.nokia.com/Community/Wiki/Como_desenvolver_um_jogo_em_Java_ME_-_Parte_1
package NovoGame;
import NovoGame.Arkanoid.ArkanoidJogo;
import NovoGame.Blocos.BlocosJogo;
import java.util.Date;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Menu extends MIDlet implements CommandListener{
    private Alert sairAlerta;
    private boolean somLigado;
    private ChoiceGroup somChoice;
    private Command cmdSairNao;
    private Command cmdSairSim;
    private Command comSelecione;
    private Command comVoltar;
    private Form ajudaForm;
    private Form configuracoesForm;
    private Form topForm;
    private List inicioForm;
    private Pontuacao[] pontos;
    private ArkanoidJogo arkanoid;
    private BlocosJogo blocos;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
//</editor-fold>//GEN-END:|fields|0|
    public Menu(){}
    public void commandAction(Command com, Displayable display){
        if(display == inicioForm){
            if(com == comSelecione){
                switch(inicioForm.getSelectedIndex()){
                    case (0):
                        display(initArkanoid());
                        break;
                    case (1):
                        display(initBlocos());
                        break;
                    case (2):
                        display(initConfiguracoesForm());
                        break;
                    case (3):
                        display(initPontuacaoForm());
                        break;
                    case (4):
                        display(initAjudaForm());
                        break;
                    case (5):
                        display(initSairForm());
                        break;
                }
            }
        }else if(display == ajudaForm){
            if(com == comVoltar){
                display(initInicioForm());
            }
        }else if(display == configuracoesForm){
            if(com == comVoltar){
                somLigado = somChoice.getSelectedIndex() == 0;
                display(initInicioForm());
            }
        }else if(display == topForm){
            if(com == comVoltar){
                display(initInicioForm());
            }
        }else if(display == arkanoid){
            if(com == comVoltar){
                sairJogo(arkanoid);
                display(initInicioForm());
            }
        }else if(display == blocos){
            if(com == comVoltar){
                sairJogo(blocos);
                display(initInicioForm());
            }
        }else if(display == sairAlerta){
            if(com == cmdSairSim){
                sair();
            }else{
                display(initInicioForm());
            }
        }
    }
    private void configuracoesIniciais(){
        somLigado = true;
        if(pontos == null){
            pontos = new Pontuacao[10];
        }
        for (int i =0; i < pontos.length; i++){
            pontos[i] = new Pontuacao("Vazio", 0, new Date());
        }
    }
    public void destroyApp(boolean unconditional){}
    public void display(Displayable display){
        Display.getDisplay(this).setCurrent(display);
    }
    public void display(Alert alert, Displayable display) {
        // shows mainForm in the screen.
        Display.getDisplay(this).setCurrent(alert, display);
    }
    public Display getDisplay(){
        return Display.getDisplay(this);
    }
    public boolean isSomLigado(){
        return somLigado;
    }
    private Displayable initAjudaForm(){
        if(ajudaForm == null){
            ajudaForm = new Form("Ajuda");
            ajudaForm.append("Use os cursores para mover a barra inferior. Não deixe a bola passar.");
            ajudaForm.setCommandListener(this);
            ajudaForm.addCommand(initVoltar());
        }
        return ajudaForm;
    }
    private Displayable initConfiguracoesForm(){
        if(configuracoesForm == null){
            configuracoesForm = new Form("Configurações");
            configuracoesForm.addCommand(initVoltar());
            configuracoesForm.setCommandListener(this);
            somChoice = new ChoiceGroup("Som", List.EXCLUSIVE);
            somChoice.append("Ligado", null);
            somChoice.append("Desligado", null);
            configuracoesForm.append(somChoice);
        }
        return configuracoesForm;
    }
    private Displayable initInicioForm(){
        if(inicioForm == null){
            inicioForm = new List("Menu", List.IMPLICIT);
            inicioForm.append("Arkanoid", null);
            inicioForm.append("Blocos", null);
            inicioForm.append("Opções", null);
            inicioForm.append("Melhores", null);
            inicioForm.append("Ajuda", null);
            inicioForm.append("Sair", null);
            comSelecione = new Command("Selecione", Command.ITEM, 1);
            inicioForm.setSelectCommand(comSelecione);
            inicioForm.setCommandListener(this);
        }
        return inicioForm;
    }
    private Displayable initArkanoid(){
        if(arkanoid != null){
            arkanoid = null;
        }
        arkanoid = new ArkanoidJogo(false, this);
        arkanoid.addCommand(initVoltar());
        arkanoid.setCommandListener(this);
        arkanoid.start();
        return arkanoid;
    }
    private Displayable initBlocos(){
        if(blocos != null){
            blocos = null;
        }
        blocos = new BlocosJogo(false, this);
        blocos.addCommand(initVoltar());
        blocos.setCommandListener(this);
        blocos.start();
        return blocos;
    }
    private void sairJogo(ArkanoidJogo tela){
        if(tela != null){
            tela.stop();
        }
    }
    private void sairJogo(BlocosJogo tela){
        if(tela != null){
            tela.stop();
        }
    }
    private Displayable initPontuacaoForm(){
        if(topForm == null){
            topForm = new Form("Melhores");
            topForm.setCommandListener(this);
            topForm.addCommand(initVoltar());
        }
        topForm.deleteAll();
        for(int i=0; i<pontos.length; i++){
            topForm.append(((i<9)? "0": "")+(i+1)+" - "+pontos[i].getNome()+": "+pontos[i].getValor()+"\n");
        }
        return topForm;
    }
    private Displayable initSairForm(){
        if(sairAlerta == null){
            sairAlerta = new Alert("Atenção", "Sair do programa?", null, AlertType.INFO);
            cmdSairSim = new Command("Sim", Command.OK, 1);
            cmdSairNao = new Command("Não", Command.BACK, 1);
            sairAlerta.addCommand(cmdSairSim);
            sairAlerta.addCommand(cmdSairNao);
            sairAlerta.setCommandListener(this);
        }
        return sairAlerta;
    }
    public Command initVoltar() {
    if (comVoltar == null) {
      comVoltar = new Command("Voltar", Command.BACK, 1);
    }
    return comVoltar;
  }
    public void sair(){
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }
    protected void startApp() throws MIDletStateChangeException{
        configuracoesIniciais();
        display(initInicioForm());
    }
    protected void pauseApp(){
    }
    public void fimJogo(int vidas, int pontos, int tempo){
        if (vidas == 0) {
            display(new Alert("Fim do jogo!", "Você perdeu!", null, AlertType.INFO), initInicioForm());
        } else {
            display(new Alert("Fim do jogo!", "Você ganhou!", null, AlertType.INFO), initInicioForm());
        }
    }
//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize(){//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet(){//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet(){//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display; if
     * <code>null</code>, then
     * <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable){//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if(alert == null){
            display.setCurrent(nextDisplayable);
        }else{
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|
}